package com.wsc.wsckeeper.activities.fragments;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.database.SQLException;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.CardActivity;
import com.wsc.wsckeeper.activities.fragments.base.BaseFragment;
import com.wsc.wsckeeper.adapter.CardsAdapter;
import com.wsc.wsckeeper.enums.Mode;
import com.wsc.wsckeeper.enums.OrderBy;
import com.wsc.wsckeeper.managers.ScreenManager;
import com.wsc.wsckeeper.model.entity.CardE;
import com.wsc.wsckeeper.persistence.dao.CardDAO;

public class CardsFragment extends BaseFragment {
	private List<CardE> cards;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
		listView.invalidateViews();
	}

	public void sort(final String field) {
		final OrderBy order = model.getOrder();

		Collections.sort(cards, new Comparator<CardE>() {
			public int compare(CardE c1, CardE c2) {

				if (order.equals(OrderBy.ASC)) {
					if (CardE.FIELD_NUMBER.equals(field))
						return c1.getNumber().compareTo(c2.getNumber());
					else if (CardE.FIELD_DESCRIPTION.equals(field))
						return c1.getDescription().compareTo(c2.getDescription());
					else if (CardE.FIELD_FK_CURRENCY.equals(field))
						return c1.getCurrency().getDescription().compareTo(c2.getCurrency().getDescription());
					return c1.getCreated().compareTo(c2.getCreated());
				} else {
					if (CardE.FIELD_NUMBER.equals(field))
						return c2.getNumber().compareTo(c1.getNumber());
					else if (CardE.FIELD_DESCRIPTION.equals(field))
						return c2.getDescription().compareTo(c1.getDescription());
					else if (CardE.FIELD_FK_CURRENCY.equals(field))
						return c2.getCurrency().getDescription().compareTo(c1.getCurrency().getDescription());
					return c2.getCreated().compareTo(c1.getCreated());
				}
			}
		});

		model.setOrder(order.equals(OrderBy.ASC) ? OrderBy.DESC : OrderBy.ASC);

		onResume();
	}

	@Override
	public void initValues() {
		cards = runtime.getCards();
		if (cards == null) {
			try {
				cards = CardDAO.queryForAll();
				runtime.setCards(cards);
			} catch (SQLException e) {
				showExceptionAsDialog(e);
			} catch (Exception e) {
				showExceptionAsDialog(e);
			}
		}

		if (cards == null)
			cards = new ArrayList<CardE>();

		model.setOrder(OrderBy.ASC);
		sort(CardE.FIELD_NUMBER);

		listView.setAdapter(new CardsAdapter(getActivity(), cards));
		registerForContextMenu(listView);
	}

	@Override
	public void initListeners() {
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				model.setCard(cards.get(position));
				model.setCardMode(Mode.SHOW);
				ScreenManager.getInstance().forward(getActivity(), CardActivity.class);
			}
		});
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		CardE card = cards.get(info.position);
		switch (item.getItemId()) {
		case R.id.context_menu_edit: {
			model.setCard(cards.get(info.position));
			model.setCardMode(Mode.EDIT);
			ScreenManager.getInstance().forward(getActivity(), CardActivity.class);
			break;
		}
		case R.id.context_menu_delete: {
			try {
				runtime.getCards().remove(card);
				CardDAO.delete(card);
				listView.invalidateViews();
			} catch (java.sql.SQLException e) {
				showExceptionAsDialog(e);
			} catch (Exception e) {
				showExceptionAsDialog(e);
			}
			break;
		}
		}
		return true;
	}
}
